﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Text;
using Cloak.Reflection;

namespace Ruleweaver.Rules.CheckMethods
{
	public sealed class EndsWithMethod : SingleTypeCheckMethod
	{
		private readonly string _value;
		private readonly StringComparison? _comparison;

		public EndsWithMethod(string value)
		{
			Contract.Requires(!String.IsNullOrEmpty(value));

			_value = value;
		}

		public EndsWithMethod(string value, StringComparison comparison) : this(value)
		{
			_comparison = comparison;
		}

		protected override Type TargetType
		{
			get { return typeof(string); }
		}

		protected override MethodInfo GetMethod(Type checkType)
		{
			return _comparison == null
				? Reflect.Func<ICheckable<string>, string, Check<string>>(Checkable.EndsWith)
				: Reflect.Func<ICheckable<string>, string, StringComparison, Check<string>>(Checkable.EndsWith);
		}

		protected override IEnumerable<object> GetCheckArguments()
		{
			yield return _value;

			if(_comparison != null)
			{
				yield return _comparison.Value;
			}
		}
	}
}